package com.vcredit.creditcat.service.activeMQ;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * Created by liaoshengjun on 2017/10/26.
 * activeMQ消息队列生产者公共类
 */
@Service
public class CommonQueueProducerService {
	@Resource(name="jmsQueueTemplate")
	private JmsTemplate jmsTemplate;
	private static Logger logger = LoggerFactory.getLogger(CommonQueueProducerService.class);
	/**
	 * 向指定队列发送消息
	 */
	public void sendMessage(Destination destination, final String msg) {
		logger.info("向队列" + destination.toString() + "发送了消息------------" + msg);
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(msg);
			}
		});
	}

	/**
	 * 向默认队列发送消息
	 */
//	public void sendMessage(final String msg) {
//		String destination =  jmsTemplate.getDefaultDestination().toString();
//		jmsTemplate.send(new MessageCreator() {
//			public Message createMessage(Session session) throws JMSException {
//				return session.createTextMessage(msg);
//			}
//		});
//
//	}
}
